package com.baic.ibase.config;


import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.integration.annotation.ServiceActivator;
import org.springframework.integration.channel.DirectChannel;
import org.springframework.integration.mqtt.core.MqttPahoClientFactory;
import org.springframework.integration.mqtt.outbound.MqttPahoMessageHandler;
import org.springframework.messaging.MessageChannel;

@Configuration
public class MqttOutboundConfiguration {

    @Autowired
    private MqttPropertiesConfig mqttPropertiesConfig;

    @Autowired
    private MqttPahoClientFactory mqttClientFactory;


    @Bean
    public MessageChannel mqttOutboundChannel() {
        return new DirectChannel();
    }

    @Bean
    @ServiceActivator(inputChannel = "mqttOutboundChannel")
    public MqttPahoMessageHandler mqttOutboundHandler() {
        MqttPahoMessageHandler handler = new MqttPahoMessageHandler("mqtt-publisher-server", mqttClientFactory);

        handler.setAsync(true);
        handler.setDefaultQos(mqttPropertiesConfig.getPublishQos());
        return handler;
    }

}
